
$(window).on('resize',function(){
    var width = document.documentElement.clientWidth
    var fontSize = width/750*100
    $('html').css('font-size',fontSize)
})
$(function(){
    document.documentElement.style.fontSize = document.documentElement.clientWidth / 750 *100 +'px'

    let strIndexOf = location.href.indexOf('referralCode=')
    if(strIndexOf > 0){
        let referralCode = location.href.substring(location.href.indexOf('referralCode=')+13)
        $('#referralCode').val(referralCode)
    }

    var message = $("#message")
    message.html('')
    $('#referralCode').on('blur',referralCodeFn)
    $('#cellphone').on('blur',cellphoneFn)
    $('#verificationCode').on('blur',verificationCodeFn)
    $('#download').on('click',function(){
        location.href = 'https://uweika.oss-cn-shenzhen.aliyuncs.com/app/zjhb.apk'
    })

    //提交
    $('#fromBtn').on('click',function(){
        if(!referralCodeFn()){
            return false
        }
        if(!cellphoneFn()){
            return false
        }
        if(!verificationCodeFn()){
            return false
        }
        var data = {
            referralCode:$('#referralCode').val(),
            cellphone:$('#cellphone').val(),
            verificationCode:$('#verificationCode').val()
        }
        var url = location.protocol+'//'+location.host
        $.ajax({
            url:url+'/ka-api/passport/accesstokens?grantType=ftt',
            type:'POST',
            data:data,
            success:function(res){
                
                if(res.code == 200){
                    alert('注册成功')
                }else{
                    alert(res.msg)
                }
            }
        })
    })


    //获取验证码
    $('#codeBtn').on('click',function(){
        let val = $('#cellphone').val()
        if(val.length == 11){
            let time = null
            clearInterval(time)
            let t = 60
            time = setInterval(function(){
                t--
                $('#codeBtn').hide()
                $('#codeBtn2').show()
                $('#codeBtn2').html(t)
                
                if(t<=0){
                    $('#codeBtn2').hide()
                    $('#codeBtn').show()
                    $('#codeBtn').html('获取验证码')
                    clearInterval(time)
                }
            },1000)
            var url = location.protocol+'//'+location.host
            var data = {
                cellphone:$('#cellphone').val()
            }
            $.ajax({
                url:url+'/ka-api/passport/verificationcodes',
                type:'POST',
                data:data,
                success:function(res){
                    if(res.data.code == 200){
                       
                    }else {
                        alert(res.data.msg)
                    }
                }
            })
            return true
        }else {
            message.html('请输入正确手机号')
            return false
        }
    })

    //邀请码验证
    function referralCodeFn(){
        let val = $('#referralCode').val()
        if(val =='' || val.trim().length==0){
            message.html('邀请码不能为空')
            return false
        }else {
            message.html('')
            return true
        }
    }

    //手机号验证
    function cellphoneFn(){
        let reg = /^[1][3,4,5,6,7,8,9][0-9]{9}$/;
        let val = $('#cellphone').val()
        if(val =='' || val.trim().length==0){
            message.html('手机号不能为空')
            return false
        }else if(!reg.test(val)){
            message.html('手机号错误')
            return false
        }else {
            message.html('')
            return true
        }
    }
    //验证码
    function verificationCodeFn(){
        let reg = /^\d{4,6}$/
        let val = $('#verificationCode').val()
        if(val =='' || val.trim().length==0){
            message.html('验证码不能为空')
            return false
        }else if(!reg.test(val)){
            message.html('验证码不正确')
            return false
        }else {
            message.html('')
            return true
        }
    }

})